#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
#

import platform

from common.const import RpcParamKey
from common.schemas.thrift_framework import ResourceScope

if platform.system().lower() == "windows":
    from common.logger_wins import Logger
else:
    from common.logger import Logger
log = Logger().get_logger()


def lock_backup_job_resource(resource_id, job_id, invoke_rpc_tool_interface):
    """
    日志转全量，加任务锁
    """
    input_param = {
        'scope': ResourceScope.PRODUCTION_RESOURCE,
        'scopeKey': '',
        'resourceKey': resource_id,
        'resourceValue': '',
        'lockType': 'w',
        'sharedNum': None,
        'jobId': job_id
    }
    try:
        result = invoke_rpc_tool_interface(job_id, RpcParamKey.LOCK_JOB_RESOURCE, input_param)
    except Exception as err_info:
        log.error(f"lock_backup_job_resource.{err_info}")
        return False
    log.info(f"lock_backup_job_resource, result {result}")
    return result.get('message', {}).get('isSuccess', False)
